﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Newtonsoft.Json;

public partial class Bk_GGPh : Basic
{
    public string strList = "";
    public string strPage = " ";
    public string strLoc = "沪A";
    protected void Page_Load(object sender, EventArgs e)
    {
        strList = sbList().ToString();

        int type = OTO.ToInt(Request["t"], 1);
        switch (type)
        {
            case 1:
                strLoc = "沪A";
                break;
            case 2:
                strLoc = "深A ";
                break;
            case 3:
                strLoc = "中小板";
                break;
            case 4:
                strLoc = "创业板";
                break;
            case 5:
                strLoc = "沪深300 ";
                break;
        }

    }

    protected StringBuilder sbList()
    {
        StringBuilder sb = new StringBuilder();
        try
        {
            int type = OTO.ToInt(Request["t"], 1);
            if (type < 1 || type > 5)
                type = 1;

            List<GgPhEnty> list = new List<GgPhEnty>();
            switch (type)
            {
                case 2:
                    list = JsonConvert.DeserializeObject<List<GgPhEnty>>(Pub.LoadData(jsonUrl + "hangqing/gglb.ashx?flag=sa"));
                    break;
                case 3:
                    list = JsonConvert.DeserializeObject<List<GgPhEnty>>(Pub.LoadData(jsonUrl + "hangqing/gglb.ashx?flag=zxb"));
                    break;
                case 4:
                    list = JsonConvert.DeserializeObject<List<GgPhEnty>>(Pub.LoadData(jsonUrl + "hangqing/gglb.ashx?flag=cyb"));
                    break;
                case 5:
                    list = JsonConvert.DeserializeObject<List<GgPhEnty>>(Pub.LoadData(jsonUrl + "hangqing/gglb.ashx?flag=hs300"));
                    break;
                default:
                    list = JsonConvert.DeserializeObject<List<GgPhEnty>>(Pub.LoadData(jsonUrl + "hangqing/gglb.ashx?flag=ha"));
                    break;
            }

            string d = OTO.ToStr(Request["d"], "");
            string d2 = OTO.ToStr(Request["d2"], "");
            string d3 = OTO.ToStr(Request["d3"], "");
            string d4 = OTO.ToStr(Request["d4"], "");
            string d5 = OTO.ToStr(Request["d5"], "");
            string d6 = OTO.ToStr(Request["d6"], "");
            string t = type.ToString();
            string o = OTO.ToStr(Request["o"], "0");
            string o2 = OTO.ToStr(Request["o2"], "1");
            string o3 = OTO.ToStr(Request["o3"], "1");
            string o4 = OTO.ToStr(Request["o4"], "1");
            string o5 = OTO.ToStr(Request["o5"], "1");
            string o6 = OTO.ToStr(Request["o6"], "1");

            if (string.IsNullOrEmpty(d) && string.IsNullOrEmpty(d2) && string.IsNullOrEmpty(d3) && string.IsNullOrEmpty(d4) && string.IsNullOrEmpty(d5) && string.IsNullOrEmpty(d6))
                d = "1";
            if (d.Equals("1"))
            {
                if (o.Equals("1"))
                    list = list.OrderByDescending(x => x.C).ToList();
                else
                    list = list.OrderBy(x => x.C).ToList();
            }
            if (d2.Equals("1"))
            {
                if (o2.Equals("1"))
                    list = list.OrderByDescending(x => x.P).ToList();
                else
                    list = list.OrderBy(x => x.P).ToList();
            }
            if (d3.Equals("1"))
            {
                if (o3.Equals("1"))
                    list = list.OrderByDescending(x => x.F).ToList();
                else
                    list = list.OrderBy(x => x.F).ToList();
            }
            if (d4.Equals("1"))
            {
                if (o4.Equals("1"))
                    list = list.OrderByDescending(x => x.F5).ToList();
                else
                    list = list.OrderBy(x => x.F5).ToList();
            }
            if (d5.Equals("1"))
            {
                if (o5.Equals("1"))
                    list = list.OrderByDescending(x => x.I).ToList();
                else
                    list = list.OrderBy(x => x.I).ToList();
            }
            if (d6.Equals("1"))
            {
                if (o6.Equals("1"))
                    list = list.OrderByDescending(x => x.Score).ToList();
                else
                    list = list.OrderBy(x => x.Score).ToList();
            }


            o = o == "1" ? "0" : "1";
            o2 = o2 == "1" ? "0" : "1";
            o3 = o3 == "1" ? "0" : "1";
            o4 = o4 == "1" ? "0" : "1";
            o5 = o5 == "1" ? "0" : "1";
            o6 = o6 == "1" ? "0" : "1";

            sb.AppendLine("<div class=\"hqzx_cpxh_lt hqzx_cpxh_tborder\">");
            sb.AppendLine("<div class=\"hqzx_cpxh_m_2_nav\">");
            sb.AppendLine("<div class=\"index_content_h h3 gray_t hqzx_cpxh_m_t_l\"><img src=\"images/index_ico2.gif\" class=\"img\" />个股数据排行  </div>");
            sb.AppendLine("<ul>");

            if (type == 1)
                sb.AppendLine("<li class=\"hqzx_cpxh_m_1_1 hqzx_cpxh_m_1_on blue\">沪A</li>");
            else
                sb.AppendLine("<li class=\"hqzx_cpxh_m_1_1\"><a href=\"Bk_GGPh.aspx?t=1\">沪A</a></li>");
            if (type == 2)
                sb.AppendLine("<li class=\"hqzx_cpxh_m_1_1 hqzx_cpxh_m_1_on blue\">深A</li>");
            else
                sb.AppendLine("<li class=\"hqzx_cpxh_m_1_1\"><a href=\"Bk_GGPh.aspx?t=2\">深A</a></li>");
            if (type == 3)
                sb.AppendLine("<li class=\"hqzx_cpxh_m_1_1 hqzx_cpxh_m_1_on blue\">中小板</li>");
            else
                sb.AppendLine("<li class=\"hqzx_cpxh_m_1_1\"><a href=\"Bk_GGPh.aspx?t=3\">中小板</a></li>");
            if (type == 4)
                sb.AppendLine("<li class=\"hqzx_cpxh_m_1_1 hqzx_cpxh_m_1_on blue\">创业板</li>");
            else
                sb.AppendLine("<li class=\"hqzx_cpxh_m_1_1\"><a href=\"Bk_GGPh.aspx?t=4\">创业板</a></li>");
            if (type == 5)
                sb.AppendLine("<li class=\"hqzx_cpxh_m_1_1 hqzx_cpxh_m_1_on blue\">沪深300</li>");
            else
                sb.AppendLine("<li class=\"hqzx_cpxh_m_1_1\"><a href=\"Bk_GGPh.aspx?t=5\">沪深300</a></li>");
            //if (type == 6)
            //    sb.AppendLine("<li class=\"hqzx_cpxh_m_1_1 hqzx_cpxh_m_1_on blue\">流出板块</li>");
            //else
            //    sb.AppendLine("<li class=\"hqzx_cpxh_m_1_1\"><a href=\"?t=6\">流出板块</a></li>");

            sb.AppendLine("</ul>");
            sb.AppendLine("<div class=\"r_time gray\">&nbsp;</div>");
            sb.AppendLine("</div>");
            sb.AppendLine("<div class=\"tableLarge\">");

            sb.AppendLine("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\" id=\"mainBtable\">");
            sb.AppendLine("<thead>");
            sb.AppendLine("<tr class=\"tableLargeHead\">");
            sb.AppendLine("<td>排序</td>");
            sb.AppendFormat("<td><a href=\"Bk_GGPh.aspx?d=1&t={0}&o={1}\">代码{2}</a></td>", t, o, ud(d, o));
            sb.AppendLine("<td>股票名称</td>");
            sb.AppendFormat("<td><a href=\"Bk_GGPh.aspx?d2=1&t={0}&o2={1}\">成交{2}</a></td>", t, o2, ud(d2, o2));
            sb.AppendFormat("<td><a href=\"Bk_GGPh.aspx?d3=1&t={0}&o3={1}\">涨跌幅{2}</a></td>", t, o3, ud(d3, o3));
            sb.AppendFormat("<td><a href=\"Bk_GGPh.aspx?d4=1&t={0}&o4={1}\">５日涨跌{2}</a></td>", t, o4, ud(d4, o4));
            sb.AppendFormat("<td><a href=\"Bk_GGPh.aspx?d5=1&t={0}&o5={1}\">换手率{2}</a></td>", t, o5, ud(d5, o5));
            sb.AppendLine("<td>成交额</td>");
            sb.AppendLine("<td>所属行业</td>");
            sb.AppendLine("<td>千股千评</td>");
            sb.AppendFormat("<td><a href=\"Bk_GGPh.aspx?d6=1&t={0}&o6={1}\">体检评分{2}</a></td>", t, o6, ud(d6, o6));
            sb.AppendLine("</tr>");
            sb.AppendLine("</thead>");
            sb.AppendLine("<tbody> ");

            int _totalCount = 0;
            int _pagesize = 30;
            int _totalPage = 0;
            _totalCount = list.Count;
            if (_totalCount > 0)
            {
                if ((_totalCount % _pagesize) == 0)
                    _totalPage = _totalCount / _pagesize;
                else
                    _totalPage = (int)(_totalCount / _pagesize) + 1;
            }
            int page = OTO.ToInt(Request["page"], 1);
            if (page == 0)
                page = 1;
            if (page > _totalPage)
                page = _totalPage;
            int _index = (page - 1) * _pagesize;
            int _end = page * _pagesize;
            if (page == _totalPage && (_totalCount % _pagesize) != 0)
                _end = (page - 1) * _pagesize + _totalCount % _pagesize;

            for (int i = _index; i < _end; i++)
            {
                sb.AppendFormat("<tr>");
                sb.AppendFormat("<td>{0}</td>", i + 1);
                sb.AppendFormat("<td><a href=\"gg{0}.shtml\" class=\"b\">{0}</a></td>", list[i].C);
                sb.AppendFormat("<td><a href=\"gg{1}.shtml\" class=\"b\">{0}</a></td>", list[i].N, list[i].C);
                sb.AppendFormat("<td><span ph=\"zdf\">{0}</span></td>", OTO.PerRedGre(list[i].P, 2, "-", ""));
                sb.AppendFormat("<td><span ph=\"zdf5\">{0}</span></td>", OTO.PerRedGre(list[i].F, 2, "-", "%"));
                sb.AppendFormat("<td><span ph=\"qr\">{0}</span></td>", OTO.PerRedGre(list[i].F5, 2, "-", "%"));
                sb.AppendFormat("<td><span ph=\"hsl\">{0}</span></td>", OTO.CoUt(list[i].I, 1, "-"));
                sb.AppendFormat("<td class=\"red\"><span ph=\"cje\">{0}</span></td>", OTO.CoUt(list[i].A, 100000000, "-", 2));
                sb.AppendFormat("<td class=\"\">{0}</td>", list[i].Hymc);
                sb.AppendFormat("<td>{0}</td>", list[i].Qgqp);
                sb.AppendFormat("<td><span class=\"red\">{0}</span></td>", OTO.PerRedGre(list[i].Score, 0, "-", ""));
                sb.AppendFormat("</tr>");
            }
            sb.AppendLine("</tbody>");
            sb.AppendLine("</table>");

            if (_totalPage > 1)
            strPage = "<div class=\"news_next\">" + PageHelper.fy(_totalCount, _pagesize, page, Request.Url.AbsoluteUri) + "</div>";

            sb.AppendLine("</div>");
            sb.AppendLine("</div>");


        }
        catch
        { }
        return sb;
    }

    protected string ud(string d, string o)
    {
        string rel = "";
        if (d == "1")
        {
            if (o == "1")
                rel = "<span class=\"green\">↓</span>";
            else if (o == "0")
                rel = "<span class=\"red\">↑</span>";
            else
                rel = "";
        }
        return rel;
    }
}